/**
 * Project:   warnme-app
 * File:      Prefs.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      23-03-2014
 */

package dtu.ds.warnme.app.application;

import org.apache.commons.lang3.StringUtils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import dtu.ds.warnme.app.ws.client.restful.UpdateRestClientOnPreferenceChangeListener;

public class Prefs {

	public static class Defaults {

		public static final String SERVER_HOST = "10.0.2.2";

		public static final boolean SERVER_INCOMING_MD5 = false;

		public static final boolean SERVER_OUTGOING_MD5 = false;

		public static final int SERVER_PORT = 8443;

		public static final int MAP_RADIUS = 5000;

		public static final boolean MAP_ALWAYS_WARN = false;

		public static final String SERVER_REALM = "warnme";

		public static final String SERVER_WEBSERVICE_CONTEXT_PATH = "/rest";

		public static final String USER_PASSWORD_HASH = "";

		public static final String USER_USERNAME = "";
	}

	public static class Keys {

		public static final String SERVER_HOST = "server.host";

		public static final String SERVER_INCOMING_MD5 = "server.incoming_md5";

		public static final String SERVER_OUTGOING_MD5 = "server.outgoing_md5";

		public static final String SERVER_PORT = "server.port";

		public static final String SERVER_REALM = "server.realm";

		public static final String MAP_RADIUS = "map.radius";

		public static final String MAP_ALWAYS_WARN = "map.always_warn";

		public static final String SERVER_WEBSERVICE_CONTEXT_PATH = "server.ws_context_path";

		public static final String USER_PASSWORD_HASH = "user.pass_hash";

		public static final String USER_USERNAME = "user.username";
	}

	private static SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ApplicationState
	        .getApplicationState());

	private static UpdateRestClientOnPreferenceChangeListener preferenceChangeListener = new UpdateRestClientOnPreferenceChangeListener();

	static {
		sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
	}

	public static String getHost() {
		return sharedPreferences.getString(Keys.SERVER_HOST, Defaults.SERVER_HOST);
	}

	public static String getPasswordHash() {
		return sharedPreferences.getString(Keys.USER_PASSWORD_HASH, Defaults.USER_PASSWORD_HASH);
	}

	public static int getPort() {
		return Integer.parseInt(sharedPreferences.getString(Keys.SERVER_PORT, String.valueOf(Defaults.SERVER_PORT)));
	}

	public static int getRadius() {
		return Integer.parseInt(sharedPreferences.getString(Keys.MAP_RADIUS, String.valueOf(Defaults.MAP_RADIUS)));
	}

	public static String getRealm() {
		return sharedPreferences.getString(Keys.SERVER_REALM, Defaults.SERVER_REALM);
	}

	public static SharedPreferences getSharedpreferences() {
		return sharedPreferences;
	}

	public static SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}

	public static String getUsername() {
		return sharedPreferences.getString(Keys.USER_USERNAME, Defaults.USER_USERNAME);
	}

	public static String getWsContextPath() {
		return sharedPreferences.getString(Keys.SERVER_WEBSERVICE_CONTEXT_PATH, Defaults.SERVER_WEBSERVICE_CONTEXT_PATH);
	}

	public static boolean isAlwaysWarn() {
		return sharedPreferences.getBoolean(Keys.MAP_ALWAYS_WARN, Defaults.MAP_ALWAYS_WARN);
	}

	public static boolean isCheckIncomingMD5() {
		return sharedPreferences.getBoolean(Keys.SERVER_INCOMING_MD5, Defaults.SERVER_INCOMING_MD5);
	}

	public static boolean isCheckOutgoingMD5() {
		return sharedPreferences.getBoolean(Keys.SERVER_OUTGOING_MD5, Defaults.SERVER_OUTGOING_MD5);
	}

	public static boolean isCredentialsProvided() {
		return StringUtils.isNoneBlank(getUsername(), getPasswordHash());
	}

	public static boolean setAlwaysWarn(boolean enabled) {
		return sharedPreferences.edit().putBoolean(Keys.MAP_ALWAYS_WARN, enabled).commit();
	}

	public static boolean setCheckIncomingMD5(boolean enabled) {
		return sharedPreferences.edit().putBoolean(Keys.SERVER_INCOMING_MD5, enabled).commit();
	}

	public static boolean setCheckOutgoingMD5(boolean enabled) {
		return sharedPreferences.edit().putBoolean(Keys.SERVER_OUTGOING_MD5, enabled).commit();
	}

	public static boolean setHost(String host) {
		return sharedPreferences.edit().putString(Keys.SERVER_HOST, host).commit();
	}

	public static boolean setPasswordHash(String passwordHash) {
		return sharedPreferences.edit().putString(Keys.USER_PASSWORD_HASH, passwordHash).commit();
	}

	public static boolean setPort(int port) {
		return sharedPreferences.edit().putInt(Keys.SERVER_PORT, port).commit();
	}

	public static boolean setRadius(int radius) {
		return sharedPreferences.edit().putInt(Keys.MAP_RADIUS, radius).commit();
	}

	public static boolean setRealm(String realm) {
		return sharedPreferences.edit().putString(Keys.SERVER_REALM, realm).commit();
	}

	public static boolean setUsername(String username) {
		return sharedPreferences.edit().putString(Keys.USER_USERNAME, username).commit();
	}

	public static boolean setWebserviceContextPath(String contextPath) {
		return sharedPreferences.edit().putString(Keys.SERVER_WEBSERVICE_CONTEXT_PATH, contextPath).commit();
	}

}
